package com.caipiao.crawler.run;

import com.caipiao.constant.CrawlerConstants;
import com.caipiao.crawler.daletou.DaletouResultPage;
import com.caipiao.crawler.daletou.DaletouResultPipeline;
import com.caipiao.crawler.football.FootballDetailPipeline;
import com.caipiao.crawler.football.FootballMatchPipeline;
import com.caipiao.crawler.football.FootballResultPage;
import com.caipiao.crawler.football.FootballResultPipeline;
import com.caipiao.crawler.mapper.LotteryFootballMapper;
import com.caipiao.util.DBTools;
import org.apache.ibatis.session.SqlSession;
import us.codecraft.webmagic.Spider;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by adminitrator on 2018/7/28.
 */
public class Football implements RunCrawler {

    @Override
    public void run() {
        Spider.create(new FootballResultPage())
                .addUrl(CrawlerConstants.FOOTBALL_RESULT_URL)
                .addPipeline(new FootballResultPipeline())
                .thread(1)
                .run();
    }

    @Override
    public void runHistory() {
//        List<String> urls = new ArrayList<String>();
//        for (int i = 18058; i < 18087; i++) {
//            urls.add(CrawlerConstants.DALETOU_DATA_URL + i);
//        }
//        Spider.create(new DaletouResultPage())
//                .addUrl(urls.toArray(new String[0]))
//                .addPipeline(new DaletouResultPipeline())
//                .thread(1)
//                .run();
    }

    public void runDetails() {
        List<String> urls = new ArrayList<String>();
        SqlSession session = DBTools.getSession();
        LotteryFootballMapper mapper = session.getMapper(LotteryFootballMapper.class);
        List list = mapper.selectByStatus();
        for(int i=0;i<list.size();i++){
            urls.add(CrawlerConstants.FOOTBALL_DETAILS_URL + list.get(i));
        }
        Spider.create(new FootballResultPage())
                .addUrl(urls.toArray(new String[0]))
                .addPipeline(new FootballDetailPipeline())
                .thread(1)
                .run();
    }

    public void runNowMatch(){
        Spider.create(new FootballResultPage())
                .addUrl(CrawlerConstants.FOOTBALL_NOW_URL)
//                .addUrl(CrawlerConstants.FOOTBALL_NOW1_URL)
                .addPipeline(new FootballMatchPipeline())
                .thread(1)
                .run();
    }





}
